package com.tdd.api.server.base.returns;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author: TDD
 * @Description:统一返回格式
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "RespEntity")
@Data
@Setter
@Getter
public class RespEntity<T>  implements Serializable {

    @ApiModelProperty(value = "返回状态码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public RespEntity(){
    }

    public RespEntity(RespCode rc,T data) {
        this.code = rc.getCode();
        this.message = rc.getMessage();
        this.data = data;
    }

    public RespEntity(RespCode rc){
        this.code = rc.getCode();
        this.message = rc.getMessage();
    }

    public RespEntity(Integer code,String msg){
        this.code = code;
        this.message = msg;
    }

    public static <T> RespEntity success(RespCode respCode,T data) {
        return new RespEntity(respCode,data);
    }

    public static <T> RespEntity success(T data) {
        return new RespEntity(RespCode.SUCCESS,data);
    }

    public static <T> RespEntity success() {
        return new RespEntity(RespCode.SUCCESS);
    }

    public static <T> RespEntity serverError(T data) {
        return new RespEntity(RespCode.SERVER_ERROR,data);
    }

    public static <T> RespEntity serverError() {
        return new RespEntity(RespCode.SERVER_ERROR);
    }

    public static <T> RespEntity failure(RespCode respCode,T data) {
        return new RespEntity(respCode,data);
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "RespEntity{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
